package com.example.team.dao.Imp;

import com.example.team.dao.IRoleDao;
import com.example.team.entity.Role;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 *作者：代志雄
 * 2023年10月31日22点54分
 */
@Repository
public class IRoleDaoImpl implements IRoleDao {
    @Autowired
    private JdbcTemplate jdbcTemplate;
    @Override
    public int insertRole(Role role) {
        return jdbcTemplate.update(
                "insert into role(roleId,roleName,jobId,departmentId) " +
                        "values(?,?,?,?)"
                ,role.getRoleId()
                ,role.getRoleName()
                ,role.getJobId()
                ,role.getDepartmentId()
        );
    }

    @Override
    public int deleteRole(Role role) {
        return jdbcTemplate.update(
                "delete from role " +
                "where RoleID=?",role.getRoleId());

    }

    @Override
    public int updateRole(Role role) {
        return jdbcTemplate.update("update role set " +
                "RoleID=?,RoleName=?,jobID=?,DepartmentId"
                ,role.getRoleId()
                ,role.getRoleName()
                ,role.getJobId()
                ,role.getDepartmentId()
        );
    }

    @Override
    public List<Role> getAllRole(Role role) {
        if(role.getRoleName()!=null)
            return jdbcTemplate.query("select " +
                "RoleID,RoleName,jobID " +
                "from role where RoleName='"+role.getRoleName()+"'",new Object[]{},
                new BeanPropertyRowMapper<>(Role.class));
        else
            return jdbcTemplate.query("select " +
                "RoleID,RoleName,jobID " +
                "from role",new Object[]{},
                new BeanPropertyRowMapper<>(Role.class));
    }

    @Override
    public Role getRole(Role role) {
        List<Role> result=jdbcTemplate.query("select " +
                                "roleId,roleName,jobId,departmentId" +
                                "from role where RoleName='"+role.getRoleName()+"'",
                        new Object[]{},
                        new BeanPropertyRowMapper<>(Role.class));
        if (result.size()>=1)
            return result.get(0);
        else return null;
    }
}
